package com.flyiu.data.consistency.config;

import java.util.Map;

import javax.persistence.EntityManager;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * 
 * <p>
 * </p>
 *
 * @author flyiu@flyiu.com
 * @version $Id: DataConsistencyJpaConfig.java, v 0.1 2019年6月23日 下午7:15:17 flyiu Exp $
 */
@EnableJpaRepositories(basePackages = "com.flyiu.data.consistency.repository", transactionManagerRef = "consistencyTransactionManager", entityManagerFactoryRef = "entityManagerFactorySecondary")
@EntityScan(basePackages = "com.flyiu.data.consistency.model")
//@EnableJpaAuditing
//@Configuration
@EnableTransactionManagement(order = 1000)
public class DataConsistencyJpaConfig {

    @Autowired
    private JpaProperties       jpaProperties;

    @Autowired
    private HibernateProperties hibernateProperties;


    @Bean(name = "consistencyDataSourceProperties")
    @Qualifier("consistencyDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.consistency")
    public DataSourceProperties consistencyDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "consistencyDatasources")
    @Qualifier("consistencyDatasources")
    @ConditionalOnMissingBean(name = "consistencyDatasources")
    @ConfigurationProperties(prefix = "spring.datasource.consistency")
    public DataSource consistencyDatasources(@Qualifier("consistencyDataSourceProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean(name = "entityManagerSecondary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryBar(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerFactorySecondary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBar(EntityManagerFactoryBuilder builder) {

        Map<String, Object> properties = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());

        return builder.dataSource(consistencyDatasources(consistencyDataSourceProperties())).packages("com.flyiu.data.consistency.model").persistenceUnit("secondaryPersistenceUnit").properties(properties).build();
    }

    @Bean(name = "consistencyTransactionManager")
    PlatformTransactionManager transactionManagerBar(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryBar(builder).getObject());
    }

}
